package com.restaurant.validators;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.restaurant.domain.Menu;
import com.restaurant.domain.Menucategory;
import com.restaurant.service.MenuService;

public class MenuCategoryDeleteValidator implements Validator{
	
	@Autowired
	private MenuService objMenuService;
	
	@Override
	public boolean supports(Class<?> arg0) {
		return Menucategory.class.isAssignableFrom(arg0);
	}

	@Override
	public void validate(Object arg0, Errors errors) {
		Menucategory menucategory = (Menucategory) arg0;
		ArrayList<Menu> listMenus = null;
		try {
			listMenus = (ArrayList<Menu>) objMenuService.loadByHql("from Menu where isdeleted = false and menucategory.menucategoryID = " + menucategory.getMenucategoryID());
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(listMenus.size()>0){
			errors.rejectValue("alreadyexist", "menucategory.in.use");			
		}
	}
}
